﻿using System;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.ReactiveUI;
using DynamicData;
using MilkTeaMachine.OfflineClient.EntityFrameworkCore.Models;
using MilkTeaMachine.OfflineClient.Models;
using MilkTeaMachine.OfflineClient.Utils;
using MilkTeaMachine.OfflineClient.ViewModels;
using ReactiveUI;

namespace MilkTeaMachine.OfflineClient.Views;

public partial class IngredientInputUserControl : ReactiveUserControl<IngredientInputViewModel>
{
    public IngredientInputUserControl()
    {
        AvaloniaXamlLoader.Load(this);
        this.WhenActivated(_ => { });
    }

    private void DataGrid_OnRowEditEnded(object? sender, DataGridRowEditEndedEventArgs e)
    {
        if (e.EditAction == DataGridEditAction.Cancel)
            return;
        if (e.Row.DataContext is not IngredientAddressVo ingredientAddressVo) return;
        if (ViewModel!.IngredientObservableCollection.Count(vo =>
                !string.IsNullOrWhiteSpace(vo.IngredientName) &&
                vo.IngredientName.Equals(ingredientAddressVo.IngredientName.Trim())) > 1)
        {
            MessageBoxUtil.GetBox("原料名重复，请重新输入").ShowWindowAsync();
            ingredientAddressVo.IngredientName = string.Empty;
            e.Row.Background = new SolidColorBrush(Colors.Red);
            return;
        }

        //添加
        if (ingredientAddressVo.Id == -1)
        {
            var add = new Ingredient
            {
                IngredientName = ingredientAddressVo.IngredientName,
                Manufacturer = ingredientAddressVo.Manufacturer,
                Description = ingredientAddressVo.Description,
                DeviceAddress = ingredientAddressVo.DeviceAddress,
            };
            var added = ViewModel!.IngredientObservableCollection.FirstOrDefault(vo =>
                vo.Sequence == ingredientAddressVo.Sequence && !string.IsNullOrWhiteSpace(vo.IngredientName));
            if (added is not null)
            {
                added.IngredientName = add.IngredientName;
                added.Manufacturer = add.Manufacturer;
                added.Description = add.Description;
            }

            ViewModel!.AddIngredients.Add(add);
            if (!string.IsNullOrWhiteSpace(add.IngredientName) &&
                !ViewModel!.IngredientObservableCollection.Any(vo => string.IsNullOrWhiteSpace(vo.IngredientName)))
                ViewModel!.IngredientObservableCollection.Add(new IngredientAddressVo
                {
                    IngredientName = "",
                    DeviceAddress = -1,
                    Sequence = ingredientAddressVo.Sequence + 1,
                    Id = -1
                });
            UpdateCollection();
            e.Row.Background = new SolidColorBrush(Colors.White);
            return;
        }

        //修改
        var update = ViewModel!.Ingredients.First(ingredient => ingredient.Id == ingredientAddressVo.Id);
        update.IngredientName =
            ViewModel!.Ingredients.Any(ingredient => ingredient.IngredientName == ingredientAddressVo.IngredientName)
                ? ""
                : ingredientAddressVo.IngredientName;
        update.Manufacturer = ingredientAddressVo.Manufacturer;
        update.Description = ingredientAddressVo.Description;
        update.DeviceAddress = ingredientAddressVo.DeviceAddress;
        ViewModel!.UpdateIngredients.Add(update);
        e.Row.Background = new SolidColorBrush(Colors.White);
    }

    private void UpdateCollection()
    {
        ViewModel!.IngredientListLeft.Clear();
        ViewModel!.IngredientListRight.Clear();
        ViewModel!.IngredientListLeft.AddRange(ViewModel!.IngredientObservableCollection.Skip(ViewModel!.SkipCount)
            .Take(IngredientInputViewModel.LeftCount));
        ViewModel!.IngredientListRight.AddRange(ViewModel!.IngredientObservableCollection
            .Skip(ViewModel!.SkipCount + IngredientInputViewModel.LeftCount)
            .Take(IngredientInputViewModel.LeftCount));
    }
}